package com.sne.monitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sne.common.entity.Result;
import com.sne.monitor.entity.MonitorTimeTask;
import com.sne.monitor.service.IMonitorTimeTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 在线任务监控 控制器
 * @author fanghh
 * @date 2020/12/7
 */
@Validated
@RestController
@RequestMapping("/monitor/monitor-time-task")
@Api(tags = "定时任务")
public class MonitorTimeTaskController {
    @Autowired
    private IMonitorTimeTaskService service;

    @PreAuthorize("hasAuthority('monitor:time-task:page')")
    @ApiOperation(value = "定时任务列表")
    @PostMapping("/page")
    public Result page(Page page, @RequestBody MonitorTimeTask timeTask) {
        return Result.success(service.findPage(page,timeTask));
    }

    @SneakyThrows
    @PreAuthorize("hasAuthority('monitor:time-task:save')")
    @ApiOperation(value = "保存定时任务")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody MonitorTimeTask timeTask) {
        return Result.success(service.saveTask(timeTask));
    }

    @PreAuthorize("hasAuthority('monitor:time-task:update')")
    @ApiOperation(value = "更新定时任务")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody MonitorTimeTask timeTask) {
        return Result.success(service.updateTask(timeTask));
    }

    @PreAuthorize("hasAuthority('monitor:time-task:delete')")
    @ApiOperation(value = "删除定时任务")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(service.deleteById(id));
    }

    @ApiOperation(value = "获取定时任务")
    @GetMapping("/find/{id}")
    public Result find(@PathVariable("id") Long id) {
        return Result.success(service.getById(id));
    }

    @PreAuthorize("hasAuthority('monitor:time-task:deleteIds')")
    @ApiOperation(value = "批量删除定时任务")
    @DeleteMapping("/deleteIds")
    public Result deleteIds(@RequestBody @Size(min = 1,message = "ids不能为空") List<Long> ids) {
        return Result.success(service.deleteByIds(ids));
    }

    @PreAuthorize("hasAuthority('monitor:time-task:changeState')")
    @ApiOperation(value = "修改定时任务状态")
    @PostMapping("/changeState")
    public Result changeState(@RequestBody MonitorTimeTask task){
        return Result.success(service.changeState(task));
    }

    @PreAuthorize("hasAuthority('monitor:time-task:run')")
    @ApiOperation(value = "修改定时任务状态")
    @GetMapping("/run/{id}")
    public Result run(@PathVariable Long id){
        return Result.success(service.run(id));
    }

}
